//
//  HQChooseReasonViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/12/26.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HQChooseReasonViewController.h"
#import "Model_Hr_CodeReasonModel.h"
@interface HQChooseReasonViewController ()
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong)NSArray *dataArray;

@property (nonatomic, strong)UITableView *tableview;

@property (nonatomic, strong)NSMutableArray *deleteArr;

@property (nonatomic ,strong) UIButton *dismissButton;

@end

@implementation HQChooseReasonViewController

- (instancetype)initWithModelArray:(NSArray *)array
{
    if (self) {
       
        self.dataArray = array;
        
        self.deleteArr = [NSMutableArray new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
//    self.navigationController.navigationBar.hidden = NO;
    
    self.navigationController.navigationBar.barTintColor = kNavBlueColor;
    
    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};

    UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(didSaveButttonClicked:)];
    self.navigationItem.rightBarButtonItem = execbtn;
    

    
    [self.view addSubview:self.tableview];
    
    [self.view addSubview:self.dismissButton];
}

- (UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight ) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.editing = YES;
        _tableview.allowsMultipleSelectionDuringEditing = YES;
        _tableview.tableFooterView = [[UIView alloc]init];
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableview;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    Model_Hr_CodeReasonModel *model = self.dataArray[indexPath.row];
    
    cell.textLabel.text = model.reason_name;
    
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//是否可以编辑  默认的时YES
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//选择编辑的方式,按照选择的方式对表进行处理
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
    }
    
}

//选择你要对表进行处理的方式  默认是删除方式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}


//选中时将选中行的在self.dataArray 中的数据添加到删除数组self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Model_Hr_CodeReasonModel *model = self.dataArray[indexPath.row];
    
    [self.deleteArr addObject:model];
    
    NSLog(@"self.deleteArr:%@", self.deleteArr);
    
}
//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    Model_Hr_CodeReasonModel *model = self.dataArray[indexPath.row];
    
    [self.deleteArr removeObject:model];
    
    NSLog(@"self.deleteArr:%@", self.deleteArr);
    
}

- (void)didSaveButttonClicked:(UIButton *)sender

{
    self.getBlocks (self.deleteArr);
    
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (UIButton *)dismissButton
{
    if (!_dismissButton) {
        _dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_dismissButton setTitle:@"取消" forState:UIControlStateNormal];
        _dismissButton.backgroundColor = kNavBlueColor;
        _dismissButton.frame = CGRectMake(10, ScreenHeight - 40 - NavBarHeight, ScreenWidth - 20, 35);
        _dismissButton.layer.masksToBounds = YES;
        _dismissButton.layer.cornerRadius = 5;
        [_dismissButton addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dismissButton;
}
- (void)dismiss:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

@end
